package de.bloxel.world.weather;

import com.jme3.asset.AssetManager;
import com.jme3.effect.EmitterSphereShape;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh.Type;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Plane;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Node;

/**
 * A point particle effect for rain.
 * 
 * See http://jmonkeyengine.org/groups/contribution-depot-jme3/forum/topic/simple-rain-effect/
 */
public class Rain extends Node {

  private static final long serialVersionUID = -1057124936652689175L;
  private ParticleEmitter points;
  private final AssetManager assetManager;
  private final Camera cam;
  private final int height = 200;

  public Rain(final AssetManager assetManager, final Camera cam, final int weather) {
    super("rain");
    this.assetManager = assetManager;
    this.cam = cam;
    applyParameters(weather);
    attachChild(points);
  }

  public void applyParameters(final int weather) {
    points = new ParticleEmitter("rainPoints", Type.Triangle, 800 * weather);
    points.setShape(new EmitterSphereShape(Vector3f.ZERO, 600f));
    // points.setLocalTranslation(new Vector3f(0f, height, 0f));
    points.setInitialVelocity(new Vector3f(0.0f, -1.0f, 0.0f));
    // points.setMaximumAngle(3.1415927f);
    // points.setMinimumAngle(0.0f);
    points.setImagesX(1);
    points.setImagesY(1);
    points.setGravity(1159.9f * weather);
    // points.setLowLife(1626.0f);
    points.setLowLife(2);
    points.setHighLife(5);
    points.setStartSize(1.8f);
    points.setEndSize(1.6f);
    points.setStartColor(new ColorRGBA(0.0f, 0.0f, 1.0f, 0.8f));
    points.setEndColor(new ColorRGBA(0.8f, 0.8f, 1.0f, 0.6f));
    // points.setRandomAngle(randomAngle)Mod(0.0f);
    points.setFacingVelocity(false);
    // points.setFaceNormal(new Vector3f(0,0,1));
    points.setParticlesPerSec(80000 * weather);
    // points.setVelocityVariation(20.0f);
    // points.setInitialVelocity(0.58f);
    points.setRotateSpeed(0.0f);
    points.setShadowMode(ShadowMode.CastAndReceive);
    final Material mat = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
    mat.setTexture("Texture", assetManager.loadTexture("Textures/Weather/RainDrop.png"));
    points.setMaterial(mat);
    points.setQueueBucket(Bucket.Transparent);
    points.updateLogicalState(0);
    points.updateGeometricState();
  }

  @Override
  public void updateLogicalState(final float tpf) {
    super.updateLogicalState(tpf);
    final float far = 800f;
    final Vector3f loc = new Vector3f(cam.getLocation());
    final Plane piano = new Plane(loc, far);
    final Ray ray = new Ray(loc, cam.getDirection());
    final Vector3f intersection = new Vector3f(cam.getLocation());
    ray.intersectsWherePlane(piano, intersection);
    intersection.y = height;
    this.setLocalTranslation(intersection);
  }
}